<template>
  <el-dialog :visible.sync="dialogVisible" :title="curId ? '编辑用户' : '新增用户'" width="700px" :before-close="close">
    <el-form
      ref="ruleForm"
      label-position="left  "
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="userName" for="userName">
        <el-input id="userName" v-model="ruleForm.userName" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" prop="fullName" for="fullName">
        <el-input id="fullName" v-model="ruleForm.fullName" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile" for="mobile">
        <el-input id="mobile" v-model="ruleForm.mobile" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email" for="email">
        <el-input id="email" v-model="ruleForm.email" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentID">
        <treeselect
          v-model="ruleForm.departmentID"
          :options="departmentsData"
          style="width: 178px"
          placeholder="选择部门"
        />
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select v-model="ruleForm.roles" multiple placeholder="选择角色">
          <el-option
            v-for="item in rolesData"
            :key="item.name"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="button-right">
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getUser, updateUser, createUser } from '@/api/system/users'
import { getRolesOption } from '@/api/system/roles'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'CuForm',
  components: { Treeselect },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    curId: {
      type: Number,
      default: null
    },
    departmentsData: {
      type: Array,
      default: () => {
      }
    }

  },

  data() {
    return {
      ruleForm: {
        userName: '',
        fullName: '',
        mobile: '',
        email: '',
        departmentID: null,
        roles: null
      },
      rolesData: [],
      rules: {
        userName: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
          { validator: this.validateNoWhitespace, trigger: 'blur' }
        ],
        mobile: [{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }],
        email: [{ type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }]
      }
    }
  },
  watch: {
    dialogVisible(v) {
      if (v) {
        if (this.curId) {
          // 编辑
          getUser(this.curId).then(res => {
            this.ruleForm = res.data
          })
        }
        this.getRoles()
      }
    }
  },
  methods: {
    validateNoWhitespace(rule, value, callback) {
      const trimmedValue = value.trim()
      if (trimmedValue === '') {
        callback(new Error('不能仅包含空格'))
      } else {
        callback()
      }
    },
    close() {
      this.$refs.ruleForm.resetFields()
      this.$emit('close')
    },
    search() {
      this.close()
      this.$emit('search')
    },
    getRoles() {
      getRolesOption().then(res => {
        this.rolesData = res.data
      })
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.curId) {
            if (!this.ruleForm.departmentID) {
              this.ruleForm.departmentID = null
            }
            updateUser(this.ruleForm).then((res) => {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.search()
            })
          } else {
            createUser(this.ruleForm).then((res) => {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.search()
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped>
.button-right {
  text-align: right;
  padding-right: 50px;
}
</style>
